package com.kitty.record.kittyjava.javaproxy;

import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import javax.sound.midi.Soundbank;
import java.beans.EventHandler;
import java.lang.reflect.Method;
import java.util.ArrayList;

/**
 * @Description
 * @Author SHEN
 * @Date 2021/1/19
 */
public class CglibProxy {

    public static void main(String[] args) {

        Enhancer enhancer = new Enhancer();
        //cglib动态代理不需要实例化被代理对象

        enhancer.setSuperclass(NewStar.class);
        /*enhancer.setCallback((MethodInterceptor) (o, method, objects, methodProxy) -> {
            if(o instanceof LuHan){
                System.out.println("我是鹿晗，我什么也不会");
            }

            if(o instanceof ZhangXueYou){
                System.out.println("我是张学友，歌神就是我");
            }

            if(o instanceof NewStar){
                System.out.println("我是超新星");
            }

            //Object invoke = methodProxy.invokeSuper(o, objects);

            return null;
        });*/
        //同时cglib的动态代理生成的对象是继承了被代理对象
        NewStar o = (NewStar) enhancer.create();

        o.dance();

        /*Class<?> superclass = o.getClass().getSuperclass();
        //superclass.getName() = com.kitty.record.kittyjava.javaproxy.LuHan
        System.out.println("superclass.getName() = " + superclass.getName());*/
    }
}

class LuHan{
    public String sing() {
        System.out.println("我是鹿晗，我会唱歌");
        return "123";
    }
    public void dance() {
        System.out.println("我是鹿晗，我会跳舞");
    }
}

class ZhangXueYou{
    public String sing() {
        System.out.println("我是张学友，我会唱歌");
        return "456";
    }
    public void dance() {
        System.out.println("我是张学友，我会跳舞");
    }
}

interface NewStar{
    public String sing();
    public void dance();
}

